unit Helpers.Strings;

interface

uses
  System.SysUtils;

type
  TStringHelper = record helper for string
  public
    function ToInt32: Int32; inline;
    function ToInt64: Int64; inline;
    function ToFloat: Double; inline;
    function ToCurrency: Currency; inline;
    function ToDateTime: TDateTime; inline;
    function ToDate: TDate; inline;
    function ToTime: TTime; inline;

    function ToInt32Def(const pDefault: Int32 = 0): Int32; inline;
    function ToInt64Def(const pDefault: Int64 = 0): Int64; inline;
    function ToFloatDef(const pDefault: Double = 0): Double; inline;
    function ToCurrencyDef(const pDefault: Currency = 0): Currency; inline;
    function ToDateTimeDef(const pDefault: TDateTime): TDateTime; inline;
    function ToDateDef(const pDefault: TDate): TDate; inline;
    function ToTimeDef(const pDefault: TTime): TTime; inline;

    function TryToInt32(out pValor: Int32): Boolean; inline;
    function TryToInt64(out pValor: Int64): Boolean; inline;
    function TryToFloat(out pValor: Double): Boolean; inline;
    function TryToCurrency(out pValor: Currency): Boolean; inline;
    function TryToDateTime(out pValor: TDateTime): Boolean; inline;
    function TryToDate(out pValor: TDate): Boolean;
    function TryToTime(out pValor: TTime): Boolean;

    function Length: Integer; inline;
    function Copy(const pPrimeiroCaracter, pQuantidadeCaracteres: Integer): string; inline;

    function Left(const pQuantidadeCaracteres: Integer): string; inline;
    function Right(const pQuantidadeCaracteres: Integer): string; inline;
  end;

implementation

uses
  System.StrUtils;

{ TStringHelper }

function TStringHelper.Copy(const pPrimeiroCaracter, pQuantidadeCaracteres: Integer): string;
begin
  Result := System.Copy(Self, pPrimeiroCaracter, pQuantidadeCaracteres);
end;

function TStringHelper.Left(const pQuantidadeCaracteres: Integer): string;
begin
  Result := System.StrUtils.LeftStr(Self, pQuantidadeCaracteres);
end;

function TStringHelper.Length: Integer;
begin
  Result := System.Length(Self);
end;

function TStringHelper.Right(const pQuantidadeCaracteres: Integer): string;
begin
  Result := System.StrUtils.RightStr(Self, pQuantidadeCaracteres);
end;

function TStringHelper.ToCurrency: Currency;
begin
  Result := System.SysUtils.StrToCurr(Self);
end;

function TStringHelper.ToCurrencyDef(const pDefault: Currency): Currency;
begin
  Result := System.SysUtils.StrToCurrDef(Self, pDefault);
end;

function TStringHelper.ToDate: TDate;
begin
  Result := System.SysUtils.StrToDate(Self);
end;

function TStringHelper.ToDateDef(const pDefault: TDate): TDate;
begin
  Result := System.SysUtils.StrToDateDef(Self, pDefault);
end;

function TStringHelper.ToDateTime: TDateTime;
begin
  Result := System.SysUtils.StrToDateTime(Self);
end;

function TStringHelper.ToDateTimeDef(const pDefault: TDateTime): TDateTime;
begin
  Result := System.SysUtils.StrToDateTimeDef(Self, pDefault);
end;

function TStringHelper.ToFloat: Double;
begin
  Result := System.SysUtils.StrToFloat(Self);
end;

function TStringHelper.ToFloatDef(const pDefault: Double): Double;
begin
  Result := System.SysUtils.StrToFloatDef(Self, pDefault);
end;

function TStringHelper.ToInt32: Int32;
begin
  Result := System.SysUtils.StrToInt(Self);
end;

function TStringHelper.ToInt32Def(const pDefault: Int32): Int32;
begin
  Result := System.SysUtils.StrToIntDef(Self, pDefault);
end;

function TStringHelper.ToInt64: Int64;
begin
  Result := System.SysUtils.StrToInt64(Self);
end;

function TStringHelper.ToInt64Def(const pDefault: Int64): Int64;
begin
  Result := System.SysUtils.StrToInt64Def(Self, pDefault);
end;

function TStringHelper.ToTime: TTime;
begin
  Result := System.SysUtils.StrToTime(Self);
end;

function TStringHelper.ToTimeDef(const pDefault: TTime): TTime;
begin
  Result := System.SysUtils.StrToTimeDef(Self, pDefault);
end;

function TStringHelper.TryToCurrency(out pValor: Currency): Boolean;
begin
  Result := System.SysUtils.TryStrToCurr(Self, pValor);
end;

function TStringHelper.TryToDate(out pValor: TDate): Boolean;
var
  lValor: TDateTime;
begin
  Result := System.SysUtils.TryStrToDate(Self, lValor);
  pValor := lValor;
end;

function TStringHelper.TryToDateTime(out pValor: TDateTime): Boolean;
begin
  Result := System.SysUtils.TryStrToDateTime(Self, pValor);
end;

function TStringHelper.TryToFloat(out pValor: Double): Boolean;
begin
  Result := System.SysUtils.TryStrToFloat(Self, pValor);
end;

function TStringHelper.TryToInt32(out pValor: Int32): Boolean;
begin
  Result := System.SysUtils.TryStrToInt(Self, pValor);
end;

function TStringHelper.TryToInt64(out pValor: Int64): Boolean;
begin
  Result := System.SysUtils.TryStrToInt64(Self, pValor);
end;

function TStringHelper.TryToTime(out pValor: TTime): Boolean;
var
  lValor: TDateTime;
begin
  Result := System.SysUtils.TryStrToTime(Self, lValor);
  pValor := lValor;
end;

end.
